package com.mastercard.assets.web.struts.actions;

import com.mastercard.assets.web.util.to.UserLogin;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class GetContratoByFileName 
        extends
		Action
{
    
    public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
    {
        
          HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		String fileName = request.getParameter( "cveCampania" ) ;
                
                // en caso de que exista
		// toma el businesCase
		
		String dirPath = getServlet( ).getServletContext( ).getRealPath( "/WEB-INF/contrato/" ) ;
                
                File f = new File( dirPath ) ;
		
		System.out.println( "Directorio: " + f.getAbsolutePath( ) ) ;
		File[ ] files = f.listFiles( ) ;
		
		File file = null ;
                
                String contratoNameBase = "Contrato-" + fileName ;
                
                // recorre
		for( File fs : files )
		{
			System.out.println( "Compara: " + fs.getAbsolutePath( ) + " con " + contratoNameBase ) ;
			// en caos e que empiece por el nombre adecuado
			if( fs.getName( ).startsWith( contratoNameBase ) )
			{
				file = fs ;
				
				break ;
			}
		}
                
                System.out.println( "FileName: " + file ) ;
		
		if( file != null )
		{
			// guarda el archivo en /WEB-INF/bc
			String contratoNamePath = file.getAbsolutePath( ) ;
			
			// toma el File
			System.out.println( contratoNamePath ) ;
			
			try
			{
				FileInputStream out = new FileInputStream( contratoNamePath ) ;
				byte[ ] read = new byte[ out.available( ) ] ;
				System.out.println( "Tamanio: " + read.length ) ;
				System.out.println( "Tamanio: " + read.length ) ;
				System.out.println( "content-disposition: " + "attachment; filename=" + file.getName( ) ) ;
				System.out.println( "content-type:" + "application/unknown" ) ;
				out.read( read ) ;
				out.close( ) ;
				response.setHeader( "content-disposition" , "attachment; filename=" + file.getName( ) ) ;
//				response.setHeader( "content-type" , "application/unknown" ) ;
				
				response.getOutputStream( ).write( read ) ;
				response.getOutputStream( ).close( ) ;
				return null ;
			}catch( Exception e )
			{
				return null ;
			}
		}
		else
			return null ;
        
    }
    
}
